from tkinter import *
import math as m

def one():
    root = Tk()

    c = Canvas(root,width=200,height=100)
    c.pack()

    line1=c.create_line(0,50,200,50,fill='yellow')
    line2=c.create_line(100,0,100,100,fill='red',dash=(2,4))
    #c.create_line(150,0,150,100,fill='blue',dash=4)
    rectangle= c.create_rectangle(50,25,150,75,fill='blue')

    Button(root,text='修改line1',command=lambda :\
           c.coords(line1,0,25,200,25)).pack(fill=X)
    Button(root,text='修改rectangle',command=lambda :\
           c.itemconfig(rectangle,fill='red')).pack(fill=X)
    Button(root,text='删除全部',command=lambda :\
           c.delete(ALL)).pack(fill=X)

    mainloop()

def two():
    root = Tk()

    w = Canvas(root,width=200,height=100)
    w.pack()
    
    w.create_line(0,0,200,100,fill='red',width=3)
    w.create_line(200,0,0,100,fill='red',width=3)
    w.create_rectangle(40,20,160,80,fill='green')
    w.create_rectangle(65,35,135,65,fill='yellow')

    w.create_text(100,50,text='Canvas')
    
    mainloop()

def three():
    root = Tk()
    
    w = Canvas(root,width=200,height=100)
    w.pack()

    w.create_oval(40,20,160,80,fill='yellow')
    w.create_rectangle(40,20,160,80,dash=4)
    w.create_text(100,50,text='Canvas')
    
    mainloop()

def four():
    root = Tk()

    w=Canvas(root,height=100,width=200,background='red')
    w.pack()

    center_x = 100
    center_y = 50
    r= 50

    points=[
        #左上角的点
        center_x - int(r*m.sin(2*m.pi/5)),
        center_y - int(r*m.cos(2*m.pi/5)),
        #右上角的点
        center_x + int(r*m.sin(2*m.pi/5)),
        center_y - int(r*m.cos(2*m.pi/5)),
        #左下角的点
        center_x - int(r*m.sin(m.pi/5)),
        center_y + int(r*m.cos(m.pi/5)),
        #顶点
        center_x,
        center_y - r,
        #右下角的点
        center_x + int(r*m.sin(m.pi/5)),
        center_y + int(r*m.cos(m.pi/5)),
        ]

    w.create_polygon(points,outline='green',fill='yellow')

    mainloop()

def five():

    root = Tk()
    
    w = Canvas(root,width=1000,height=200,xscrollcommand=sb.set)
    w.pack()
    
    sb = Scrollbar(root,command=w.xview,orient=HORIZONTAL)
    sb.pack(fill=X)
    
    def four(event):
        center_x = event.x
        center_y = event.y
        r= 50

        points=[
            #左上角的点
            center_x - int(r*m.sin(2*m.pi/5)),
            center_y - int(r*m.cos(2*m.pi/5)),
            #右上角的点
            center_x + int(r*m.sin(2*m.pi/5)),
            center_y - int(r*m.cos(2*m.pi/5)),
            #左下角的点
            center_x - int(r*m.sin(m.pi/5)),
            center_y + int(r*m.cos(m.pi/5)),
            #顶点
            center_x,
            center_y - r,
            #右下角的点
            center_x + int(r*m.sin(m.pi/5)),
            center_y + int(r*m.cos(m.pi/5)),
            ]

        w.create_polygon(points,outline='green',fill='yellow')
       
    w.bind("<B3-Motion>",four)
    Label(root, text="按住鼠标左键并移动，开始绘制你的理想蓝图吧......").pack(side=BOTTOM)

    sb.config(command=w.xview)
    
    mainloop()

while True:
    a=str(input('线：1，文本：2，椭圆形：3，多边形：4,制图：5，退出请按:6\n'))
    if a == '1':
        one()

    elif a == '2':
        two()
    elif a == '3':
        three()

    elif a == '4':
        four()
    elif a=='5':
        five()
    elif a=='6':
        break
